package com.weiiew.backend.operation;

import com.weiiew.backend.entity.KeyInfo;
import com.weiiew.backend.operationUtils.KeyCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * @Description
 * @Author 魏锴
 * @Date 2025/4/28
 */
@Setter
@Getter
@ToString
@AllArgsConstructor
public class CombinedKeyClick extends ComputerOperation {
    private static final long serialVersionUID = 121204894510L; // 添加 serialVersionUID
    private final KeyCode keyCode;
    private final boolean hasShift;
    private final boolean hasCtrl;
    private final boolean hasAlt;
    private final boolean hasWin;
    private final long duration;
    private final long interval;

    public CombinedKeyClick(KeyInfo info, long timestamp) {
        keyCode = info.getKeyCode();
        hasShift = info.isHasShift();
        hasCtrl = info.isHasCtrl();
        hasAlt = info.isHasAlt();
        hasWin = info.isHasWin();
        duration = info.getDuration();
        interval = info.getInterval();
        this.setTimestamp(timestamp);
    }

    @Override
    public void execute(Robot robot) {
        if (hasShift) robot.keyPress(KeyEvent.VK_SHIFT);
        if (hasCtrl) robot.keyPress(KeyEvent.VK_CONTROL);
        if (hasAlt) robot.keyPress(KeyEvent.VK_ALT);
        if (hasWin) robot.keyPress(KeyEvent.VK_WINDOWS);
        robot.keyPress(keyCode.getRobotCode());
        robot.delay((int) duration);
        robot.keyRelease(keyCode.getRobotCode());
        if (hasShift) robot.keyPress(KeyEvent.VK_SHIFT);
        if (hasCtrl) robot.keyPress(KeyEvent.VK_CONTROL);
        if (hasAlt) robot.keyPress(KeyEvent.VK_ALT);
        if (hasWin) robot.keyPress(KeyEvent.VK_WINDOWS);
    }
}
